package cn.valorin.data;

import static cn.valorin.configuration.Configuration.data;
import static cn.valorin.configuration.Configuration.dataFile;
import static cn.valorin.configuration.Configuration.saveData;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.configuration.ConfigurationSection;

import cn.valorin.Main;
import cn.valorin.util.RandomKey;

public class Data {
    public static void createPlant(String world,int x,int y,int z,int type) {
    	MySQL mysql = Main.getInstance().getMySQL();
    	boolean databaseEnable = mysql.isEnabled();
    	if (databaseEnable) {
    		mysql.createPlant(world, x, y, z, type);
    	} else {
    		String typeString;
    		if (type == 0) {
    			typeString = "Bean";
    		} else {
    			typeString = "GlutinousRice";
    		}
    		String subKey = "Plant."+typeString+"."+RandomKey.get();
			data.set(subKey+".World", world);
			data.set(subKey+".X", x);
			data.set(subKey+".Y", y);
			data.set(subKey+".Z", z);
			saveData();
    	}
    }
    
    public static void removePlant(String block_world,int block_x,int block_y,int block_z,int block_type) {
    	MySQL mysql = Main.getInstance().getMySQL();
    	boolean databaseEnable = mysql.isEnabled();
    	if (databaseEnable) {
    		mysql.removePlant(block_world, block_x, block_y, block_z, block_type);
    	} else {
    	  	String subKey = getPlantKey(block_world, block_x, block_y, block_z, block_type);
    	  	if (subKey == null) { return; }
    	  	data.set(subKey, null);
    	  	saveData();
    	}
    }
     
    public static boolean isPlant(String block_world,int block_x,int block_y,int block_z,int block_type) {
    	MySQL mysql = Main.getInstance().getMySQL();
    	boolean databaseEnable = mysql.isEnabled();
    	if (databaseEnable) {
    		return mysql.isPlant(block_world, block_x, block_y, block_z, block_type);
    	} else {
    		if (getPlantKey(block_world, block_x, block_y, block_z, block_type) == null) {
    			return false;
    		} else {
    			return true;
    		}
    	}
    }
    
    public static String getPlantKey(String block_world,int block_x,int block_y,int block_z,int block_type) {
    	String typeString;
		if (block_type == 0) {
			typeString = "Bean";
		} else {
			typeString = "GlutinousRice";
		}
    	if (!dataFile.exists()) { return null; }
		List<String> plantList = new ArrayList<String>();
		ConfigurationSection section = data.getConfigurationSection("Plant."+typeString);
	  	if (section == null) { return null; }
	  	section.getKeys(false).forEach(key->{plantList.add(key);});
	  	
	  	for (String plant : plantList) {
	  		String subKey = "Plant."+typeString+"."+plant;
	  		String world = data.getString(subKey+".World");
	  	    int x = data.getInt(subKey+".X");
	  	    int y = data.getInt(subKey+".Y");
	  	    int z = data.getInt(subKey+".Z");
	  	    if (block_world.equals(world) &&
	  	    	block_x == x &&
	  	    	block_y == y &&
	  	    	block_z == z) {
	  	    	return subKey;
	  	    }
	  	}
	  	return null;
    }
}
